#设置各种参数
#typeset MIP='127.0.0.1'
#typeset dbmpassword='123456'
# typeset SIP='10.27.3.176'               #从服务器ip
# typeset hmuser='root'                   #主服务器远程用户
# typeset hmpassword='1qaz@WSX'           #主服务器远程密码
# typeset hsuser='root'                   #从服务器远程用户
# typeset hspassword='1qaz@WSX'           #从服务器远程密码
# typeset dbsuser='root'                  #从服务器mysql用户
# typeset dbspassword='QWEasdzxc#123'     #从服务器mysql密码
# typeset sconfpath='/etc/my.cnf'         #从mysql配置文件路径


typeset MIP='@@MIP@@'               #主服务器ip
typeset dbmuser='@@dbmuser@@'                  #主服务器mysql用户
typeset dbmpassword='@@dbmpassword@@'     #主服务器mysql密码
typeset check_times='@@check_times@@'                  #命令重复次数
typeset mconfpath='@@mconfpath@@'         #主mysql配置文件路径

typeset MIP='10.27.3.175'               #主服务器ip
typeset dbmuser='root'                  #主服务器mysql用户
typeset dbmpassword='QWEasdzxc#123'     #主服务器mysql密码
typeset check_times=''                  #命令重复次数
typeset mconfpath='/etc/my.cnf'         #主mysql配置文件路径
#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer'&& log_format "ERROR" "script_execute_error!" && exit 1
fi

#配置日志路径
typeset SCRIPT_NAME="mysql_master_$MIP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
[ ! -f "${LOG_FILE}" ] && touch ${LOG_FILE}

function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

log_format "begin to build mysql master................................."

#检查配置文件是否存在
if [ ! "$mconfpath" ];then
    cnfnum=`find / -name my.cnf|wc -l`
    if [ "$cnfnum" -gt 1 ];then
        log_format 'ERROR' 'number of my.cnf is more than one,please input my.cnf path' && log_format "ERROR" "script_execute_error!" && exit 1
    else
        mconfpath=`find / -name my.cnf `
        log_format 'INFO' "find my.cnf path: $mconfpath"
    fi
else
    if [ ! -f "$mconfpath" ];then
        log_format 'ERROR' "my.cnf does not exit,check my.cnf path" && log_format "ERROR" "script_execute_error!" && exit 1
    else
        log_format 'INFO' "my.cnf exits"
    fi
fi



#备份配置文件
time=`date +%Y%m%d`
backpath=${mconfpath}_${time}.bak
if [ ! -f "$backpath" ];then
   cp $mconfpath $backpath
    if [ ! -f "$backpath" ];then
        log_format 'ERROR' "copy my.cnf failed" && log_format "ERROR" "script_execute_error!" && exit 1
    else
        log_format 'INFO' "copy my.cnf success"
    fi
else
    log_format 'INFO' "my.cnf copy exits"
fi

#检查mysql命令路径
basedir=`cat $mconfpath|grep -v ^#|grep basedir|awk -F'=' '{print $2}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
if [ ! "$basedir" ];then
    log_format 'ERROR' "can not find basedir" && log_format "ERROR" "script_execute_error!" && exit 2
else  
    mycmdpath=$basedir/bin/mysql
    if [ ! -e "$mycmdpath" ];then
        log_format 'ERROR' "can not find mysql cmd $mycmdpath" && log_format "ERROR" "script_execute_error!" && exit 2
    else
        log_format 'INFO' "mysql cmd finded $mycmdpath"
    fi
fi


#添加repl用户
log_format 'INFO' "begin to add repl user"
mycmd="$mycmdpath -h$MIP -u$dbmuser -p$dbmpassword"
checkuser=`$mycmd   -e "select * from mysql.user where User='repl'\G"`
echo $checkuser
if [ ! "$checkuser" ];then
    log_format "WARNING" "there is no reple user,create a reple user"
    $mycmd  <<eof
use mysql;
create user 'repl'@'%' identified by 'Hatech1618';
grant replication slave on *.* to 'repl'@'%';
flush privileges;
eof
    if [ $? -ne 0 ];then
        log_format "ERROR" "create reple user failed" && log_format "ERROR" "script_execute_error!" && exit 1
    else
        log_format "INFO" "reple user create success"
    fi
else
    log_format "WARNING" "warning repl user exits"
fi

log_format "INFO" "script_execute_success!"